package com.airsystem.pos.bengkel.entity.loader;

import java.util.HashMap;
import java.util.Map;

import com.airsystem.pos.bengkel.entity.Tagihan;
import com.airsystem.pos.bengkel.entity.wrapper.TagihanWrapper;
import com.airsystem.pos.bengkel.interfaces.ICallback;
import com.airsystem.pos.bengkel.interfaces.ILoader;
import com.airsystem.pos.bengkel.interfaces.IServices;
import com.airsystem.pos.bengkel.service.TagihanServices;

/**
 * @author Fanny Irawan Sutawanir (fannyirawans@gmail.com)
 */
public class TagihanLoader implements ILoader<Map<String, TagihanWrapper>> {
	private IServices<Tagihan> service;

	private Tagihan params;

	public TagihanLoader() {
		service = new TagihanServices();
	}

	public void load(ICallback<Map<String, TagihanWrapper>> callback) {
		Iterable<Tagihan> tagihans = service.select(params);

		Map<String, TagihanWrapper> map = new HashMap<String, TagihanWrapper>();

		for (Tagihan item : tagihans) {
			if (map.get(item.getPeriode()) == null) {
				map.put(item.getPeriode(), new TagihanWrapper(item));
			} else {
				map.get(item.getPeriode()).addTagihan(item);
			}
		}
		callback.get(map);

		params = null;
	}

	public void setParams(Tagihan params) {
		this.params = params;
	}
}